<?php
namespace Admin\Controller;
use Tool\AdminController;
use Think\Verify;

class ScoreController extends AdminController {
    function student() {
    	include ('top.php');
    	$student = D('Student');
    	$id = session('id');
    	$role = D('Manager')->where('id=' . $id)->find();
    	if (session('name') ==  'admin' || $role['role'] == '50'){
    		$info = $student->select();
    		$this->assign('info',$info);
    	} else {
    		$info = $student->where('teacher=' . $id)->select();
    		$this->assign('info',$info);
    	}
    	$this->display();
    }
    
    function addstudent() {
    	include ('top.php');
    	$teacher = D('Manager');
    	$t = $teacher->where('role=51')->select();
    	$this->assign('teacher',$t);
    	$student = new \Model\StudentModel();
    	if ($_GET['t_number'] == false) {
    		if (!empty($_POST)) {
		    	if (!$student->create()) {
		    		$this->assign('error',$student->getError());
		    	} else {
    				$z = $student->add($_POST);
    				if ($z) {
    					$this->redirect('Tips/tips');
    				}
		    	}
    		}
    	} else {
    			$ma = $student->where('t_number=' . $_GET['t_number'])->find();
    			$this->assign('ma',$ma);
    			if (!empty($_POST)) {
    				$student->create();
    				$error = $student->getError();
    				if (!empty($error) && $error['id_number'] != '身份证号已经存在！') {
    					dump($error);
    					$this->assign('error',$error);
    				} else {
	    				$z = $student->where('t_number=' . $_GET['t_number'])->save($_POST);
	    				if ($z) {
	    					$this->redirect('Tips/tips');
	    				}
    				}
    			}
	    	}
    	$this->display();
    }
    
    function mark() {
    	include ('top.php');
    	$manager = D('Manager');
    	$ma = $manager->where('id=' . session('id'))->find();
    	if (session('name') == 'admin' || $ma['role'] == '50') {
    		$student = D('Student')->select();
    		$this->assign('info',$student);
    	} else {
    		$id = session('id');
    		$student = D('Student')->where('teacher=' . $id)->select();
    		$this->assign('info',$student);
    	}
    	$this->display();
    }
    
    function marking() {
    	include ('top.php');
    	$stu = D('Student')->where('t_number=' . $_GET['t_number'])->find();
    	$this->assign('stu',$stu);
    	
    	$mark = D('Mark')->where('st_id=' . $_GET['t_number'])->find();
    	$que = new \Model\QuestionModel();
    	$answer = $que->readfile($mark['answer']);
    	$ans = explode('<test/>', $answer);
    	$id = explode(',', $mark['qu_id']);
    	$zz = array_combine($id,$ans);
    	$this->assign('zz',$zz);
    	
    	if (!empty($_POST)&&$_POST['mark']<=20) {
    		$m = $stu['mark']+$_POST['mark'];
    		D('Student')->mark = $m;
    		$mm = D('Student')->where('t_number=' . $_GET['t_number'])->save();
    		if ($mm) {
    			$this->redirect('Tips/tips');
    		}
    	}
    	$this->display();
    }
    
    function del() {
    	$zz = D('Student')->where('t_number=' . $_GET['t_number'])->delete();
    	if ($zz) {
    		$this->redirect('Tips/tips');
    	}
    }
}